<template>
	<view>
		<cu-custom :bgColor="'bg-'+ theme.themeColor" :isBack="false">
			<block slot="content">商品分类</block>
		</cu-custom>
		<view class="VerticalBox">
			<scroll-view class="VerticalNav nav" style="background-color: #fff;">
				<view class="cu-item" :class="index==tabCur?'cur text-'+theme.themeColor:''"
					v-for="(item,index) in list" :key="index" @tap="tabSelect(item,index)" :data-id="index">
					{{item.name}}
				</view>
			</scroll-view>
			<scroll-view class="VerticalMain" v-if="spList.length !== 0">
				<view class="list-catainer">
					<view class="list-con" v-for="childItem in spList" :key="childItem.id" @tap="routerDetail(childItem)">
						<image  class="img" mode="" :src="childItem.spuUrls[0]"></image>
						<text class="texts" v-truncate="10" >{{childItem.name}}</text>
					</view>
				</view>
			</scroll-view>
			<view class="data-con" v-else>
				<u-empty text="暂无数据" mode="list"></u-empty>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		getList
	} from '@/api/goodscategory.js';
	import {
		getPage 
	} from '@/api/goodsspu.js';
import badgeMix  from '@/mixins/tabbar-badge.js'
 export default {
	mixins: [badgeMix],
		data() {
			return {
				theme: getApp().globalData.theme,
				list: [],
				tabCur: 0,
				mainCur: 0,
				verticalNavTop: 0,
				load: true,
				spList:[]
			};
		},
		onLoad() {
			this.getGoodsCategory();
			this.defaultList()
		},
		onReady() {
			uni.hideLoading()
		},
		methods: {
			/**
			 * 获取商品分类
			 * @param {Object} 
			 */
			getGoodsCategory() {
				getList().then(res => {
					this.list = res.data
				});
			},
			/**
			 * 跳转商品列表
			 */
			
			doGoods(id) {
				uni.navigateTo({
					url: '/subpkg/goods/goods-list/index?categorySecondId=' + id
				})
			},
			async tabSelect(item,index) {
				const { data } = await getPage({
					current: '1',
					size: 10,
					descs: '',
					ascs: '',
					name: '',
					categorySecondId:item.children[0].id
				})
				
				this.spList = data.records
				
				this.tabCur = index
				
				console.log(this.spList)
				// this.tabCur = e.currentTarget.dataset.id;
				// this.mainCur = e.currentTarget.dataset.id;
				// this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50
			},
			
			//跳转详情页
			routerDetail(item){
				if(item.categorySecondId === "1498510397314220034"){
					uni.navigateTo({
						url:`/subpkg/beauty/beautyDetails/beautyDetails?id=${item.id}`
					})
				}else{
					uni.navigateTo({
						url:`/subpkg/goods/goods-detail/index?id=${item.id}`
					})
				}
			},
			async defaultList() {
				const { data } = await getPage({
					current: '1',
					size: 10,
					descs: '',
					ascs: '',
					name: '',
					categorySecondId:'1496672394983804930'
				})
				
				this.spList = data.records
				
				console.log(this.spList)
			},
			
			verticalMain(e) {
				// #ifdef MP-ALIPAY
				return false //支付宝小程序暂时不支持双向联动 
				// #endif
				let that = this;
				let tabHeight = 0;
				if (this.load) {
					for (let i = 0; i < this.list.length; i++) {
						let view = uni.createSelectorQuery().select("#main-" + i);
						view.fields({
							size: true
						}, data => {
							this.list[i].top = tabHeight;
							tabHeight = tabHeight + data.height;
							this.list[i].bottom = tabHeight;
						}).exec();
					}
					this.load = false
				}
				let scrollTop = e.detail.scrollTop + 10;
				for (let i = 0; i < this.list.length; i++) {
					if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
						this.verticalNavTop = (i - 1) * 50
						this.tabCur = i
						console.log(scrollTop)
						return false
					}
				}
			}
		},
	}
</script>

<style scoped>
	page{
		background-color: #fff;
	}
	::v-deep .u-icon--bottom{
		position: absolute;
		top: 50%;
		left: 50%;
	}
	.list-catainer{
		display: flex;
		padding: 15px 10px 0;
		justify-content: space-between;
		flex-wrap: wrap;
	}
	.list-con{
		display: flex;
		flex-direction: column;
		width: 48.5%;
		margin-bottom: 15px;
		align-items: center;
		padding: 15px 10px 0 10px;
		border-radius: 8px;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
		background-color: white;
	}
	.img{
		width: 94px;
		height: 91px;
	}
	.texts{
		font-size: 12px;
		margin: 10px 0;
	}
	.fixed {
		position: fixed;
		z-index: 99;
	}

	.VerticalNav.nav {
		width: 200upx;
		white-space: initial;
		height: 100vh;
		overflow: hidden;
		border-right: 1px solid #f1f1f1;
	}

	.VerticalNav.nav .cu-item {
		width: 100%;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
	}

	.VerticalNav.nav .cu-item.cur {
		background-color: #f1f1f1;
	}

	.VerticalNav.nav .cu-item.cur::after {
		content: "";
		width: 8upx;
		height: 30upx;
		border-radius: 10upx 0 0 10upx;
		position: absolute;
		background-color: currentColor;
		top: 0;
		right: 0upx;
		bottom: 0;
		margin: auto;
	}

	.VerticalBox {
		display: flex;
	}

	.VerticalMain {
		/* background-color: #f1f1f1; */
		flex: 1;
	}
</style>
